import os
import argparse
import subprocess
from datetime import date, timedelta, datetime
import time

import pandas as pd
from pyhive import hive


def calculate(args):
    intentions = ['PlayerValidStart',
                  'deviceOff',
                  'AppLaunch',
                  'app_start',
                  'DeviceLaunch',
                  'volumeChange',
                  ]
    yesterday_ = (date.today() + timedelta(days=- 1)).strftime("%Y-%m-%d")
    accuracy_private = calculate_for_dt(yesterday_)


def calculate_for_dt(yesterday_):
    # yesterday_ = '2023-11-08'
    dt_statistic_ = (datetime.strptime(yesterday_, '%Y-%m-%d').date() + timedelta(days=- 2)).strftime("%Y-%m-%d")
    print('-----------------------------dt_statistic_ : {}---------------------'.format(dt_statistic_))
    # 读取公共规则
    path_statistic_public_path = os.path.join(os.getcwd(), 'data', dt_statistic_ + '_' + 'public_result.csv')
    df_statistic_public = pd.read_csv(path_statistic_public_path, names=['series', 'frequency', 'probability'])
    # 读取私人规则
    path_statistic_private_path = os.path.join(os.getcwd(), 'data', dt_statistic_ + '_' + 'private_result.csv')
    df_statistic_private = pd.read_csv(path_statistic_private_path,
                                       names=['distinct_id', 'series', 'frequency', 'probability'])

    dt_online_ = (datetime.strptime(yesterday_, '%Y-%m-%d').date() + timedelta(days=- 1)).strftime("%Y-%m-%d")
    print('-----------------------------dt_online_ : {}---------------------'.format(dt_online_))
    # 读取线上数据
    df_online_path = os.path.join(os.getcwd(), 'data', dt_online_ + '_' + 'df_sample_statistic_for_dt')
    df_online = pd.read_csv(df_online_path)

    # 私人规则准确度验证
    df_statistic_private_to_verify = df_statistic_private[
        df_statistic_private['distinct_id'].isin(df_online['distinct_id'])]
    df_online_to_verify = df_online[
        df_online['distinct_id'].isin(df_statistic_private['distinct_id'])]
    # 分母是所有预测的意图，不过id要在online中先出现
    rule_all_num_private = df_statistic_private_to_verify.shape[0]

    def concat_static(row):
        return f"{row['distinct_id']}:{row['series']}"

    def concat_online(row):
        return f"{row['distinct_id']}:{row['event_info_list_str']}"

    df_private_check = df_statistic_private_to_verify.apply(concat_static, axis=1)
    df_online_check = df_online_to_verify.apply(concat_online, axis=1)
    # 分子是所有id和意图都命中的
    rule_hit_num_private = len(df_private_check[df_private_check.isin(df_online_check)])
    accuracy_private = rule_hit_num_private / rule_all_num_private
    print(
        '----------------------------------accuracy_private: {}-----------------------------'.format(accuracy_private))

    # 公共规则准确度验证
    # 分母是online的所有数据
    rule_all_num_public = df_online_to_verify.shape[0]
    # 分子是公共意图命中的条数
    rule_hit_num_public = \
        df_online_to_verify[df_online_to_verify['event_info_list_str'].isin(df_statistic_public['series'])].shape[0]

    accuracy_public = rule_hit_num_public / rule_all_num_public
    print('----------------------------------accuracy_public: {}-----------------------------'.format(accuracy_public))
    return accuracy_private
